<%@LANGUAGE="VBSCRIPT" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<!--#include file="ErrorHandler.asp" -->
<% on error resume next %>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Email Form</title>
<link href="UniversalStyleSheet.css" rel="stylesheet" type="text/css" />

<script type="text/javascript">

function OpenAddressBook(who){
	val=document.getElementById(who).value
	url="AddressBook.asp?sendback=" + who + "&val=" + val
	title="AddressBook"
	params="width=700,height=500,status=no"
	popup=window.open (url,title,params)
}

function SendEmail(){
t=document.getElementById("fTo")
c=document.getElementById("fCC")
s=document.getElementById("fSubject")
m=document.getElementById("fMessage")
r=document.getElementById("fReport")
ef=document.getElementById("EmailForm")
missing=0
missingstr=""
if (t.value ==""){
	missingstr += + " - To\n"
	missing=1
}

if (s.value ==""){
	missingstr += " - Subject\n"
	missing=1
}

if (m.value ==""){ 
	missingstr += " - Message\n"
	missing=1
}

if (r.selectedIndex ==-1 || r.selectedIndex ==0){
	missingstr += " - Report was not chosen\n"
	missing=1
}

if (missing ==1){
	alert("Cant Send Email. The Following Fields Where Left Blank:\n" + missingstr)
}
else{
	ef.action="EmailForm.asp?action=save"
	ef.submit()
}
}
</script>
</head>

<body>
<%
if request.QueryString("action")="" then
	set fs=server.CreateObject("scripting.filesystemobject")
	set ef=fs.opentextfile(server.MapPath("access/backend/email.txt"),1,true)
	e=ef.readline
	eTo=mid(e,instr(e,"=")+1)
	
	e=ef.readline
	eCC=mid(e,instr(e,"=")+1)
	
	e=ef.readline
	eSubject=mid(e,instr(e,"=")+1)
	
	e=ef.readline
	eMessage=mid(e,instr(e,"=")+1)
	
	ef.close
	set fs=nothing

else 'send email
	set fs=server.CreateObject("scripting.filesystemobject")
	set ef=fs.opentextfile(server.MapPath("access/backend/email.txt"),2,true)
	ef.writeline ("To=" & request.Form("fTo"))
	ef.writeline ("CC=" & request.Form("fCC"))
	ef.writeline ("Subject=" & request.Form("fSubject"))
	ef.writeline ("Message=" & request.Form("fMessage"))
	ef.close
	set fs=nothing
	
	'send mail configuration
	if application("EmailServer")="" then server.Execute("GetEmailServer.asp")	
	if application("EmailServer")="" then 
		%><script type="text/javascriptscript">alert("No Email Server Set. Please Advise Your Administrator.")</script><%
		response.End()
	else
		EmailServer=application("EmailServer")
	end if
		
	sch = "http://schemas.microsoft.com/cdo/configuration/" 
    
    Set cdoConfig = CreateObject("CDO.Configuration") 
    
    With cdoConfig.Fields 
		.Item(sch & "sendusing") = 2 ' cdoSendUsingPort 
    	.Item(sch & "smtpserver") = EmailServer
    	.update 
    End With 
	
	 Set cdoMessage = CreateObject("CDO.Message") 

	set email=server.CreateObject("cdo.message")
	with email
		Set .Configuration = cdoConfig
		.subject=request.Form("fSubject")
		.from="Support@Alpha-Mail.com"
		.textbody=request.Form("fMessage")
		.addattachment server.MapPath("reports") & "\" & request.Form("fReport")
		.to=request.Form("fTo")
		if request.Form("fCC")<>"" then
			.cc=request.Form("fCC")
		end if
		.Send
		if err.number<>0 then ErrorHandler("no")
	end with
	%>
	<script type="text/javascript">
	alert("Email Sent!\nTo: <%= request.form("fTo") %>\nAttached Report: <%= server.MapPath("reports") + "\" + request.Form("fReport") %> ")
	window.close()
	</script>
	<%

end if

%>
<table width="600" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td width="20" height="20" background="Images/TableUpperLeft.png"></td>
    <td width="560" height="20" background="Images/TableUpperMiddle.png"></td>
    <td width="20" height="20" background="Images/TableUpperRight.png"></td>
  </tr>
  <tr>
    <td width="20" background="Images/TableMiddleLeft.png"></td>
    <td bgcolor="#FFFFFF"><form id="EmailForm" name="EmailForm" method="post" action="">
      <table width="460" border="0" cellpadding="0" cellspacing=" 1" bgcolor="#009900">
        <tr>
          <td width="56" bgcolor="#FFFFFF" class="ResultsAddress">To:<img src="Images/addressBook.jpg" alt="address book" width="23" height="22" title="Click Here To Open The Address Book Where You Can Choose Email Recipients Or Edit / Add Address Book Entries" longdesc="Click Here To Open The Address Book Where You Can Choose Email Recipients Or Edit / Add Address Book Entries" onclick="OpenAddressBook('fTo')"/></td>
          <td width="401" bgcolor="#FFFFFF" class="ResultsAddress"><p>
            <input name="fTo" type="text" id="fTo" size="60" value="<%= eTo %>"/>
          </p>
            <p></p></td>
        </tr>
        <tr>
          <td bgcolor="#FFFFFF" class="ResultsAddress">CC:<img src="Images/addressBook.jpg" alt="address book" width="23" height="22" title="Click Here To Open The Address Book Where You Can Choose Email Recipients Or Edit / Add Address Book Entries" longdesc="Click Here To Open The Address Book Where You Can Choose Email Recipients Or Edit / Add Address Book Entries" onclick="OpenAddressBook('fCC')"/></td>
          <td bgcolor="#FFFFFF" class="ResultsAddress"><p>
            <input name="fCC" type="text" id="fCC" size="60" value="<%= eCC %>"/>
          </p>
            <p>&nbsp; </p></td>
        </tr>
        <tr>
          <td bgcolor="#FFFFFF" class="ResultsAddress">Subject:</td>
          <td bgcolor="#FFFFFF" class="ResultsAddress"><p>
            <input name="fSubject" type="text" id="fSubject" size="60" value="<%= eSubject %>"/>
          </p>
            <p>&nbsp; </p></td>
        </tr>
        <tr>
          <td bgcolor="#FFFFFF" class="ResultsAddress">Report:</td>
          <td bgcolor="#FFFFFF" class="ResultsAddress">
		  <select name="fReport" id="fReport">
		  <option value=""></option>
          <%
		  	set rs=server.CreateObject("adodb.recordset")
		  	with rs.fields
		  		.append "fName",202,200
				.append "cDate",7
			end with
			rs.open()
			set fs=server.CreateObject("scripting.filesystemobject")
		  	fldr=server.MapPath("reports")
		  	set rptfldr=fs.getfolder(fldr)
		  	for each file in rptfldr.files
		  		if fs.getextensionname(file)="xls" then 
					rs.addnew
					rs("fName")=file.name
					rs("cDate")=file.datecreated
					rs.update
				end if
			next
			rs.sort = "cDate desc"
			
			if not rs.eof then
				rs.movefirst
				do while not rs.eof %>
					<option value="<%=rs("fName") %>" title="<%= rs("fName") %>">
					<%
					if len(rs("fName"))>84 then 
						response.Write(left(rs("fName"),81)) & "..." 
					else
						response.Write(rs("fName"))
					end if
					%>
					</option>
				<% 
					rs.movenext
				loop
			end if
		  	rs.close
			set rs=nothing
			set fs=nothing
		  	%>
		  </select>
		  </td>
        </tr>
        <tr>
          <td colspan="2" bgcolor="#D2FFD2" class="ResultsAddress"></td>
          </tr>
        <tr>
          <td bgcolor="#FFFFFF" class="ResultsAddress">Message:</td>
          <td bgcolor="#FFFFFF" class="ResultsAddress"><textarea name="fMessage" cols="60" rows="10" id="fMessage"><%= eMessage %></textarea></td>
        </tr>
        <tr>
          <td colspan="2" align="right" bgcolor="#FFFFFF" class="ResultsAddress"><p></p>
            <p><img src="Images/SendButton.png" alt="Send" width="60" height="30" onclick="SendEmail()" style="cursor:pointer"/></p></td>
          </tr>
      </table>
            </form>
    </td>
    <td width="20" background="Images/TableMiddleRight.png"></td>
  </tr>
  <tr>
    <td width="20" height="20" background="Images/TableLowerLeft.png"></td>
    <td width="560" height="20" background="Images/TableLowerMiddle.png"></td>
    <td width="20" height="20" background="Images/TableLowerRight.png"></td>
  </tr>
</table>
</body>
</html>
